<template>
<view class="uni-list">
	<view class="content">
		<view class="add-left">
			<view class="uni-list-cell-left">物品名称:</view>
			<view class="uni-list-cell-db">
				<input @input="onInputName" class="uni-input" focus placeholder="输入物品名称" />
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">物品数量:</view>
				<input class="uni-input" :value="goodcount" />
			</view>
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">物品分类:</view>
				<view class="uni-list-cell-db">
					<picker @change="bindPickerCateChange" :value="cate_index" :range="catelist" range-key="title">
						<view class="uni-input">{{catelist[cate_index].title}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="img-right" @tap="get_img">
		    <image style="width: 200rpx; height: 200rpx; background-color: #eeeeee;"  :src="baseUrl + uploadimg"></image>
		</view>
	</view>
	<view>
		<view class="uni-list-cell">
			<view class="uni-list-cell-left">物品空间:</view>
			<text >{{space_name}}</text><button @tap="showTree">选择</button>
			<ba-tree-picker ref="treePicker" :multiple='false' @select-change="selectChange" title="选择空间"
				:localdata="spaceitem" valueKey="id" textKey="title" childrenKey="children" />
		</view>
		<view class="uni-list-cell">
			<view class="uni-list-cell-left">过期时间:</view>
			<view class="uni-list-cell-db">
				<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
					<view class="uni-input">{{date}}</view>
				</picker>
			</view>
		</view>
	</view>
	<view>
		<view class="uni-title uni-common-pl">物品备注信息</view>
		<view class="uni-textarea">
			<textarea class="textarea" auto-height @input="rmInput"></textarea>
		</view>
	</view>
	<button size="mini" @click="submit">保存</button>
</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import baTreePicker from "@/components/ba-tree-picker/ba-tree-picker.vue"
	export default {
		components: { baTreePicker },
		data() {
			const currentDate = this.getDate({
			            format: true
			        })
			return {
				//uploadimg: 'http://192.168.3.23/upload/20220603/89435c60af191bd5d6d2c09a7d6c9a7b.jpg ',
				uploadimg: '/static/image/plus.jpg',
				cate_index: 0,
				space_id:0,
				space_name : '未选择',
				date: currentDate,
				goodcount : 1,
				catelist:[{
					'id':0,
					'title':'物品分类'
				}],
				spaceitem:[],
				remart:'',
				baseUrl: getApp().globalData.baseUrl,
			}
		},
		onLoad() {
			uni.$on('space',res=>{
				//console.log(res)
				this.spaceitem = res.spaceitem
			})
			this.getCateList()
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),	
		    startDate() {
		        return this.getDate('start');
		    },
		    endDate() {
		        return this.getDate('end');
		    }
		},
		methods: {
			rmInput: function(e){
				//console.log(e)
				this.remart = e.detail.value
			},
			selectChange(ids, names) {
				console.log(ids, names)
				this.space_id = ids[0]
				this.space_name = names
			},
			showTree(){
				this.getSpaceList()
				this.$refs.treePicker._show();
			},
			onInputName:function(e){
				this.title = e.detail.value
			},
			bindDateChange: function(e) {
			    this.date = e.detail.value
				console.log(this.date)
			},
			bindPickerCateChange: function(e) {
			            console.log('分类选择改变，携带值为', e.detail.value)
			            this.cate_index = e.detail.value
			        },
			async getCateList() {
				const cateres= await this.$myRequest({
					url : this.baseUrl + '/api.cate/list',
					token : this.userInfo.token,
					method: 'GET',
				})
				//console.log(cateres)
				this.catelist = cateres.data.data
			},
			async getSpaceList() {
				const res= await this.$myRequest({
					url : this.baseUrl +'/api.space/getSpaceTreeList',
					token : this.userInfo.token,
					method: 'GET',
				})
				console.log(res.data.data)
				this.spaceitem = res.data.data
			},
			async submit(){
				console.log(this.catelist[this.cate_index].id)
				const res= await this.$myRequest({
					url : this.baseUrl + '/api.goods/add',
					token : this.userInfo.token,
					method: 'POST',
					data:{
						'cate_id': this.catelist[this.cate_index].id ,
						'space_id': this.space_id,
						'title': this.title,
						'logo': this.uploadimg,
						'stock': this.goodcount,
						'end_time': this.date,
						'status':'1',
						'user_id' : this.userInfo.id,
						'remark' : this.remart,
					}
				})
				console.log(res.data.code)
				if(res.data.code ===  1){
					uni.showToast({
						title : '数据添加成功！'
					})
					uni.navigateBack({
						delta:1,
					})
				}
			},
			get_img(){
				const _self = this;
				this.uploadimg = ''
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						//console.log(tempFilePaths[0])
						uni.uploadFile({
							url: this.baseUrl + '/api.UpLoad',
							method: 'POST',
							filePath: tempFilePaths[0],
							name: 'image',
							formData: {
								'user': 'test'
							},
							header: {
								'token' : this.userInfo.token,
							},
							success: (uploadFileRes) => {
								const resData = JSON.parse(uploadFileRes.data)
								console.log(resData)
								if(resData.code !== 200 ){
									uni.showToast({
										'title' : resData.msg
									})
								}
								_self.uploadimg = resData.data.url;
								console.log('upload_img:',_self.uploadimg)
							},
							fail: (err) => {
								console.log(err)
							}
						});
					}
				});
			},
			getDate(type) {
			    const date = new Date();
			    let year = date.getFullYear();
			    let month = date.getMonth() + 1;
			    let day = date.getDate();
			
			    if (type === 'start') {
			        year = year - 20;
			    } else if (type === 'end') {
			        year = 2099;
			    }
			    month = month > 9 ? month : '0' + month;
			    day = day > 9 ? day : '0' + day;
			    return `${year}-${month}-${day}`;
			},
		}
	}
</script>

<style lang="scss">
.uni-list{
	.content{
		display: flex;
		.add-left {
			width: 500rpx;
			background: #fff;
		}
		.img-right{
			width: 140rpx;
			image{
				width: 130rpx;
			}
		}
	}
}
.textarea{
	border: 1px solid grey;
}
</style>
